package me.zixing.memorandum.ui.page;

import me.zixing.memorandum.domain.DayDO;
import me.zixing.memorandum.domain.MonthDO;
import me.zixing.memorandum.ui.component.TheDayPanel;
import me.zixing.memorandum.utils.AppConstant;
import me.zixing.memorandum.utils.LunarUtils;
import me.zixing.memorandum.utils.TimingUtils;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainPage extends JFrame {

    private MonthDO monthDO;
    private JLabel previousLabel;
    private JLabel nextLabel;
    private volatile JPanel calendarMainBodyPanel;
    private JPanel calendarMainPanel;
    private JLabel dayLabel;
    private JButton todayButton;
    private JLabel lunarLable;
    private JPanel memMainPanel;
    private JScrollPane scrollPane;
    private int gridCount = 42;

    public MainPage() throws HeadlessException {
        preConfig();
        initUI();
        initAction();
    }



    private void preConfig(){
        monthDO = MonthDO.buildMonthDO(new Date());
    }

    private void initUI(){

        JPanel mainPanl = new JPanel();
        mainPanl.setSize(862,452);
        mainPanl.setBackground(Color.GRAY);
        mainPanl.setLayout(null);

        JPanel calendarPanel = new JPanel();
        calendarPanel.setBackground(AppConstant.MAIN_COLOR);
        calendarPanel.setBounds(0,0,556,520);
        calendarPanel.setLayout(null);

        JPanel calendarTopPanel = new JPanel();
        calendarTopPanel.setBounds(0,0,556,100);
        calendarTopPanel.setLayout(null);

        calendarMainPanel = new JPanel();
        calendarMainPanel.setBounds(0,102,556,350);
        calendarMainPanel.setLayout(null);

        String currentMonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        dayLabel = new JLabel(currentMonth);
        dayLabel.setFont(AppConstant.MAIN_FONT);
        dayLabel.setBounds(235,20,100,50);
        int year = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));
        int month = Integer.valueOf(new SimpleDateFormat("MM").format(new Date()));
        int day = Integer.valueOf(new SimpleDateFormat("dd").format(new Date()));
        String lunarString = LunarUtils.oneDayWithYearAndMonth(year,month,day);
        lunarLable = new JLabel(lunarString);
        lunarLable.setFont(new Font("微软雅黑",Font.PLAIN,15));
        lunarLable.setBounds(260,60,200,20);

        todayButton = new JButton("今天");
        todayButton.setFont(AppConstant.MAIN_FONT);
        todayButton.setBounds(380,30,60,30);
        todayButton.setForeground(Color.white);
        todayButton.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));

        previousLabel = new JLabel("<");
        previousLabel.setForeground(AppConstant.MAIN_COLOR);
        previousLabel.setFont(AppConstant.HEI_BOLD_20);
        previousLabel.setBounds(175,35,20,20);
        nextLabel = new JLabel(">");
        nextLabel.setForeground(AppConstant.MAIN_COLOR);
        nextLabel.setFont(AppConstant.HEI_BOLD_20);
        nextLabel.setBounds(350,35,20,20);


        JPanel calendarMainHeadPanel = new JPanel();
        calendarMainHeadPanel.setBounds(0,0,556,50);
        calendarMainHeadPanel.setLayout(new GridLayout(1,7));
        JLabel weekLabel07 = new JLabel("  日");
        weekLabel07.setForeground(AppConstant.MAIN_COLOR);
        weekLabel07.setFont(AppConstant.MAIN_FONT);
        JLabel weekLabel01 = new JLabel("  一");
        weekLabel01.setFont(AppConstant.MAIN_FONT);
        JLabel weekLabel02 = new JLabel("  二");
        weekLabel02.setFont(AppConstant.MAIN_FONT);
        JLabel weekLabel03 = new JLabel("  三");
        weekLabel03.setFont(AppConstant.MAIN_FONT);
        JLabel weekLabel04 = new JLabel("  四");
        weekLabel04.setFont(AppConstant.MAIN_FONT);
        JLabel weekLabel05 = new JLabel("  五");
        weekLabel05.setFont(AppConstant.MAIN_FONT);
        JLabel weekLabel06 = new JLabel("  六");
        weekLabel06.setForeground(AppConstant.MAIN_COLOR);
        weekLabel06.setFont(AppConstant.MAIN_FONT);
        calendarMainHeadPanel.add(weekLabel07);
        calendarMainHeadPanel.add(weekLabel01);
        calendarMainHeadPanel.add(weekLabel02);
        calendarMainHeadPanel.add(weekLabel03);
        calendarMainHeadPanel.add(weekLabel04);
        calendarMainHeadPanel.add(weekLabel05);
        calendarMainHeadPanel.add(weekLabel06);

        memMainPanel = new JPanel();
        memMainPanel.setSize(280,640);
        memMainPanel.setLayout(null);
        memMainPanel.setBackground(new Color(230,230,230));
        scrollPane = new JScrollPane();
        TimingUtils.setJViewport(scrollPane.getViewport());
        TimingUtils.start();

        //日历详情
        calendarMainBodyPanel = generateCalendarPanel();

        //备注
        JPanel memPanel = new JPanel();
        memPanel.setBounds(558,0,304,452);
        memPanel.setLayout(new BorderLayout(0,0));
        memPanel.setBackground(new Color(230,230,230));


        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        memMainPanel.setPreferredSize(new Dimension(memMainPanel.getWidth(),memMainPanel.getHeight()));
        scrollPane.getViewport().add(memMainPanel);
        memPanel.add(scrollPane);

        calendarTopPanel.add(previousLabel);
        calendarTopPanel.add(nextLabel);
        calendarTopPanel.add(dayLabel);
        calendarTopPanel.add(lunarLable);
        calendarTopPanel.add(todayButton);
        calendarPanel.add(calendarTopPanel);
        calendarMainPanel.add(calendarMainHeadPanel);
        calendarMainPanel.add(calendarMainBodyPanel);
        calendarPanel.add(calendarMainPanel);

        mainPanl.add(calendarPanel);
        mainPanl.add(memPanel);
        this.setLayout(null);
        this.add(mainPanl);
    }

    private void initAction(){
        previousLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                monthDO = monthDO.previousMonthDO();
                dayLabel.setText(monthDO.getMonthForGregorian());
                lunarLable.setText(monthDO.getMonthForLunar());
                TheDayPanel.inited = false;
                refresh(monthDO);
            }
        });
        nextLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                monthDO = monthDO.nextMonthDO();
                dayLabel.setText(monthDO.getMonthForGregorian());
                lunarLable.setText(monthDO.getMonthForLunar());
                TheDayPanel.inited = false;
                refresh(monthDO);
            }
        });
        todayButton.addActionListener(e -> {
            preConfig();
            dayLabel.setText(monthDO.getMonthForGregorian());
            lunarLable.setText(monthDO.getMonthForLunar());
            TheDayPanel.inited = false;
            refresh(monthDO);
        });

    }

    private void refresh(MonthDO monthDO){
        this.monthDO = monthDO;
        this.calendarMainPanel.remove(calendarMainBodyPanel);
        calendarMainBodyPanel = generateCalendarPanel();
        this.calendarMainPanel.add(calendarMainBodyPanel);
        this.setVisible(true);
    }

    public JPanel generateCalendarPanel(){
        JPanel calendarMainBodyPanel = new JPanel();
        calendarMainBodyPanel.setBounds(0,50,556,290);
        calendarMainBodyPanel.setLayout(new GridLayout(6,7));

        List<DayDO> dayDOS = monthDO.getDayDOS();
        int dayOfWeek = dayDOS.get(0).getDayOfWeek();
        for (int j = 0;j<dayOfWeek-1;j++){
            calendarMainBodyPanel.add(new JLabel(""));
        }


        for(int i = 0;i<dayDOS.size();i++){
            calendarMainBodyPanel.add(new TheDayPanel(dayDOS.get(i),scrollPane.getViewport()));
        }

        for (int t=dayDOS.size()+dayOfWeek;t<gridCount;t++){
            calendarMainBodyPanel.add(new JLabel(""));
        }
        return calendarMainBodyPanel;

    }

    public static void main(String[] args){
        try {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.translucencySmallShadow;
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();

            UIManager.put("RootPane.setupButtonVisible",false);
            MainPage mainPage = new MainPage();
            mainPage.setTitle("Java日历备忘录");
            mainPage.setIconImage(ImageIO.read(MainPage.class.getClassLoader().getResource("icon.png")));
            mainPage.setSize(872,489);
            mainPage.setResizable(false);
            mainPage.setLocationRelativeTo(null);
            mainPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainPage.setVisible(true);
            mainPage.setFocusable(true);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
